﻿$().ready(function () {
    getUsageData(updateTable, handleError);
    $("#clientZoneLink").click(function(event) {
        System.Shell.execute("https://clientzone.afrihost.com");
        event.preventDefault();
    });
});

function updateTable(data) {
    var tbody = $("#dataTable tbody");
    for (var idx in data) {
        var dataItem = data[idx];
        var newRow;
        if (!dataItem.isUncapped) {
            var used = dataItem.size * (dataItem.usedPercentage / 100);
            var available = dataItem.size - used;
            newRow = '<tr><td class="name">' + dataItem.name + ' <br/>(' + (dataItem.size).toFixed(0) + ' GB) </td>';
            newRow += '<td class="usage"><div id="usage_' + idx + '"></div></td>';
            newRow += '<td class="percentage">' + dataItem.usedPercentage.toFixed(2) + '%</td>';
            newRow += '<td class="available">' + available.toFixed(2) + '&nbsp;GB</td></tr>';
        } else {
            newRow = '<tr><td class="name">' + dataItem.name + '</td>';
            newRow += '<td class="usage">' + dataItem.usage + '</td>';
            newRow += '<td class="percentage">N/A</td>';
            newRow += '<td class="available">N/A</td></tr>';
        }
        
        tbody.append(newRow);

        $("#usage_" + idx).progressbar({ value: dataItem.usedPercentage });
    }
}

function handleError(message) {
    
}